//
//  PhotoLibrarySelectableCollectionViewCell.swift
//  MT
//
//  Created by M on 2019/12/28.
//  Copyright © 2019 张强. All rights reserved.
//

import UIKit
import MTCategory

class PhotoLibrarySelectableCollectionViewCell: PhotoLibarayCollectionViewCell {
    
    /// 资源
    override var asset: MTAsset? {
        didSet {
            guard let asset = asset else { return }
            selectButton.isSelected = asset.isSelected
            selectButton.isHidden = asset.mediaType == .video || isSelectButtonHidden
        }
    }
    
    /// 是否隐藏
    var isSelectButtonHidden: Bool = false {
        didSet {
            if selectButton.isHidden == isSelectButtonHidden {
                return
            }
            selectButton.isHidden = isSelectButtonHidden || asset?.mediaType == .video
        }
    }
    
    /// 选择按钮
    lazy var selectButton: UIButton = {
        let btn = UIButton()
        btn.backgroundColor = UIColor.white.withAlphaComponent(0.5)
        btn.setBackgroundImage(UIImage(named: "icon_photo_selected"), for: .selected)
        btn.layer.cornerRadius = 10
        btn.layer.borderWidth = 1
        btn.layer.borderColor = UIColor.white.cgColor
        btn.adjustsImageWhenHighlighted = false
        btn.isUserInteractionEnabled = false
        btn.addTarget(self, action: #selector(selectableCollectionViewCell(didClickSelectButton:)), for: .touchUpInside)
        return btn
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        initializeUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

// MARK: ------------------ 初始化 ----------------------
extension PhotoLibrarySelectableCollectionViewCell {
    
    private func initializeUI() {
        contentView.addSubview(selectButton)
        selectButton.snp.makeConstraints { (make) in
            make.top.equalTo(contentView).offset(5)
            make.right.equalTo(contentView).offset(-5)
            make.width.height.equalTo(20)
        }
    }
    
}

// MARK: ------------------ 事件方法 ----------------------
@objc
extension PhotoLibrarySelectableCollectionViewCell {
    
    private func selectableCollectionViewCell(didClickSelectButton sender: UIButton) {
        
    }
    
}
